#ifndef _HXSY_H
#define _HXSY_H

#if ARDUINO < 100
#include <WProgram.h>

#else
#include <Arduino.h>
#endif


class HXSY
{
public:
    byte HXNBGET();
    byte HXGETTGAM(char sw);
    unsigned char HXGETDISTENCE(unsigned char d);
    HXsetColor(unsigned char d,unsigned char s,long int color);
    bool  LIGHT_B_W(unsigned char aa,unsigned char bb,unsigned char cc);
	bool  LIGHT5_B_W(unsigned char aa,unsigned char bb,unsigned char cc);
	bool  LIGHT5_B_W_1(unsigned char aa,unsigned char bb,unsigned char cc);
    WriteSEG(unsigned char KEY2,unsigned int SEGdat);
    waitbutton(unsigned char KEY1,unsigned char swKEY1);
    bool buttonPressed(unsigned char KEY,unsigned char swKEY);
    MOTOR_STOP_ONE(unsigned char aaa);
	MOTOR_SEND(unsigned char aa,int bb);
	MOTOR_START();
	MOTOR_STOP();
	MOTOR_SEND_START(unsigned char aa,int bb);
	DJset(unsigned char rg6,unsigned char rg7,unsigned char rg8,unsigned char rg9);
	DJset1(unsigned char sw,unsigned char rg);
	DJset2(unsigned char sw,unsigned char sp,unsigned char rg,unsigned char rg2);
protected:

private:

    
    //校验相关变量
    int   generatedChecksum = 0;
    byte  checksum = 0; 
    
    //接收数据长度和数据数组
    byte  payloadLength = 0;
    byte  payloadData[32] = {0};//总共接收32个自己的数据
    
    //需要读取的信息变量
    byte signalquality = 0;//信号质量
    byte attention = 0;    //注意力值
    byte meditation = 0;   //放松度值


     unsigned char SEG[4]={0,0,0,0};
     unsigned char segbuff;
     unsigned int segcnt;
	 unsigned char MOTORcorlbuff[4] = {0,0,0,0};
     unsigned char MOTORcorl[4] = {0,0,0,0};
	 unsigned int cs100Abuff[4];
	 
	 
	//从串口读取一个字节数据
	byte ReadOneByte() 
	{
	  int ByteRead;
	  while(!Serial.available());
	  ByteRead = Serial.read();
	  return ByteRead;//返回读到的字节
	}
	 
	 void CS100A_SEND(unsigned char AA)
	 {
		 unsigned char csbuff;
		 
		 pinMode(AA, OUTPUT);	 
		 for(char i=0;i<4;i++)
		 {
		      digitalWrite(AA, LOW);//拉低总线，请求通信
		      delayMicroseconds (100) ;
		      digitalWrite(AA, HIGH);//拉低总线，请求通信
		      csbuff = cs100Abuff[i];
		       for(char j=0;j<8;j++)
		       {
		           if(csbuff & 0x80)
		           {
		               digitalWrite(AA, HIGH); 
		               delayMicroseconds (100) ;//延时100us
		           }
		           else
		           {
		               digitalWrite(AA, LOW); 
		               delayMicroseconds (100) ;//延时100us
		           }
		           csbuff = csbuff<<1; 
		       }     
		 }
		 
		  digitalWrite(AA, HIGH);
		 
	 }
	 char MOTOR_send()
	 {
			unsigned char MOTORbuff;
			unsigned char cnt = 0;
			
			
			  pinMode(2, OUTPUT);
			
			  //noInterrupts();
			  for(char i=0;i<4;i++)
			  {
				  MOTORbuff = MOTORcorl[i];
			       digitalWrite(2, LOW);//拉低总线，请求通信
			       delay(1);
			       digitalWrite(2, HIGH);//拉低总线，请求通信

			        for(char j=0;j<8;j++)
			        {
			            if(MOTORbuff & 0x80)
			            {
			                digitalWrite(2, HIGH); 
			                delayMicroseconds (85) ;//延时100us
			            }
			            else
			            {
			                digitalWrite(2, LOW); 
			                delayMicroseconds (90) ;//延时100us
			            }
			            MOTORbuff = MOTORbuff<<1; 
			        }     
			  }
			  //interrupts();
			  digitalWrite(2, HIGH);

			  
			  delay(1);

			  
			  		 
	 }
};

#endif // _HXSY_H
